//
//  STCommentsListView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/11.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STCommentsListView.h"
#import "STDoorEvaCell.h"
#import "UITableView+FDTemplateLayoutCell.h"

@implementation STCommentsListView


-(void)setCommentsListView{
    [self loadCommentsListView];
}

-(void)loadCommentsListView{
    if (!commentTableView) {
        commentTableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        commentTableView.delegate = self;
        commentTableView.dataSource = self;
        commentTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        commentTableView.backgroundColor = [UIColor clearColor];
        commentTableView.tableFooterView = [UIView new];
        [commentTableView registerClass:[STCommentCell class] forCellReuseIdentifier:@"STCommentCell"];
        WeakSelf
        commentTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf updataNewCommentList];
        }];
        
        commentTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf addMoreCommentList];
        }];
        [self addSubview:commentTableView];
        
    }else{
        [commentTableView reloadData];
    }
}

-(void)reloadTotalCell{
    [self endRefresh];
    [commentTableView reloadSection:0 withRowAnimation:UITableViewRowAnimationNone];
}
-(void)reloadCommentListView{
    [self endRefresh];
    [commentTableView reloadSection:1 withRowAnimation:UITableViewRowAnimationNone];
}

-(void)endRefresh{
    [commentTableView.mj_footer endRefreshing];
    [commentTableView.mj_header endRefreshing];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 0;
    }
    return 10;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section ==1){
        return self.oldCommentData.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 100;
    }else if (indexPath.section == 1 ){
        float height = [tableView fd_heightForCellWithIdentifier:@"STCommentCell" configuration:^(id cell) {
            [self configureCell:cell atIndexPath:indexPath];
        }];
        return height;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell;
    if (indexPath.section == 0) {
        static NSString * evaIdentifier = @"evaIdentifier";
        STDoorEvaCell * cell =[tableView dequeueReusableCellWithIdentifier:evaIdentifier];
        if (!cell) {
            cell = [[STDoorEvaCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:evaIdentifier];
        }
        [cell setEvaluateView:self.totalScore];
        cell.commentBtn.hidden = YES;
        cell.evaLab3.hidden = YES;
        cell.evaComment3.hidden = YES;
        return cell;
    }else if (indexPath.section == 1 ){
        STCommentCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"STCommentCell"];
        commentCell = (STCommentCell *)[self configureCell:commentCell atIndexPath:indexPath];
        return commentCell;
    }
    NSLog(@"sadf");
    return cell;
}

- (UITableViewCell *)configureCell:(STCommentCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    static NSString * identifier = @"STCommentCell";
     if (!cell) {
        cell = [[STCommentCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    STCommentInfoModel * dicComment = self.oldCommentData[indexPath.row];
    cell.delegate = self;
    [cell setCommentWithData:dicComment];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        if ([self.delegate respondsToSelector:@selector(commentsListViewSelNum:)]) {
            [self.delegate commentsListViewSelNum:(int)indexPath.row];
        }
    }
}

-(void)updataNewCommentList{
    [commentTableView.mj_header beginRefreshing];
    if([self.delegate respondsToSelector:@selector(commentsListUpdataNew)]){
        [self.delegate commentsListUpdataNew];
    }else{
        [self endRefresh];
    }
}


-(void)addMoreCommentList{
    [commentTableView.mj_footer beginRefreshing];
    if([self.delegate respondsToSelector:@selector(commentsListAddMore)]){
        [self.delegate commentsListAddMore];
    }else{
        [self endRefresh];
    }
}



//cell 回调
-(void)commentCell:(STCommentCell *)cell replayCommentText:(NSString *)replayStr{
    if ([self.delegate respondsToSelector:@selector(commentsListView:indexpath:replayCommentText:)]) {
        NSIndexPath * indepath = [commentTableView indexPathForCell:cell];
        [self.delegate commentsListView:self indexpath:indepath replayCommentText:replayStr];
    }
}


-(void)commentCell:(STCommentCell *)cell selImgNum:(int)selNum{
    if ([self.delegate respondsToSelector:@selector(commentsListView:indexpath:selImgNum:)]) {
        NSIndexPath * indepath = [commentTableView indexPathForCell:cell];
        [self.delegate commentsListView:self indexpath:indepath selImgNum:selNum];
    }
}




@end
